<?PHP
class db
{
		  var $hostname = "localhost";
		  var $username = "someuser";
		  var $password = "a1b2c3d4e5";
		  var $database = "ChelleWeb";
		  var $error = "";
	 private function connect()
	 {	  
		  $connection = mysql_connect($this->hostname, $this->username, $this->password);
		  if(!$connection) {
		  	$this->error .= "Could not connect: " . mysql_error() . "<br />";
		} else {
			$this->error .= "No connection error <br />";
		}
		$dbselect = mysql_select_db($this->database, $connection);
		if(!$dbselect) {
			$this->error .= "Could not select DB: " . mysql_error() . "<br />";
		} else {
			$this->error .= "No dbselect error <br />";
		}
	 }
	 private function close()
	 {
	  		mysql_close($this->connection);
	 }
	 public function get_content($page)
	 {
	 		$this->error .= "GET_CONTENT: <br />";
	 		$this->connect();
			$p = $page;
			if($p == ""){
				$p = "&nbsp;";
			}
			$sql = "SELECT page,content FROM content WHERE page = '$p'";
			$result = mysql_query($sql);
			if(!$result) {
				$this->error .= " get_content() query failed: " .mysql_error() . "<br />";
			}else {
				$result = mysql_fetch_assoc($result);
				$contRAW = $result[content];
				$cont = html_entity_decode($contRAW, ENT_QUOTES);
				return $cont;
			}
			$this->close();
	 }
	 
	 public function set_content($page, $cont)
	 /*
	 	page to change ($page) and content ($cont) is given to the function
		get_content is used to check if the page already exist.
		mysql_num_rows checks the amount of returned rows.
		IF above 0 the content in the DB is replaced with the $cont
		ELSE a new entry is created in the DB with $page and $cont
		The returned message in each case specifies what was done.
	 */
	 {
		$p = $page;
		$c = htmlentities($cont, ENT_QUOTES);
		$msg = "NOTHING HAPPEND";
		$test = $this->get_content($p);
		if($test) {
			$sql = "UPDATE content SET content='$c' WHERE page='$p'";
			$msg = $p . " already existed. Content was updated.";
		} else {
			$sql = "INSERT INTO content VALUES ('$p', '$c')";
			$msg = $p . " did not exist yet. It has been created.";
		}
		$this->error .= "SET_CONTENT: <br />";
		$this->connect();
		$result = mysql_query($sql);
		if(!$result) {
			$this->error .= "set_content() query failed: " .mysql_error() . "<br />";
		}	
		return $msg;
	 }
	public function error()
	 {
		return $this->error;
	 }
}
